package org.alvin;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.io.File;
import java.io.IOException;

public class QRDemo {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(400, 550);
        jFrame.setContentPane(new DrawPane());
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);
    }

    static class DrawPane extends JPanel {


        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = 300;
            int h = 450;
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(Color.white);

            g2d.fillRect(10, 10, w, h);
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            g2d.setPaint(new Color(45, 140, 240));
            g2d.fillRect(10, 10, w, h >> 1);

            Stroke defaultStroke = g2d.getStroke();
            int boxW = 200;
            Rectangle2D.Double rect = new Rectangle2D.Double((w - boxW) >> 1, (h - boxW) >> 1, boxW, boxW);
            //
            g2d.setPaint(new Color(255, 255, 255));
            g2d.fill(rect);

            Stroke stroke = new BasicStroke(3);
            g2d.setPaint(Color.black);
            g2d.setStroke(stroke);
            g2d.draw(rect);

            //微信，支付宝
            String text = "[扫一扫]启动充电";
            g2d.setPaint(Color.white);
            g2d.setStroke(defaultStroke);
            g2d.setFont(new Font("楷体", Font.BOLD, 23));
            int textWidth = g2d.getFontMetrics().stringWidth(text);
            g2d.drawString(text, (w - textWidth) >> 1, ((h - boxW - g2d.getFontMetrics().getHeight()) >> 1));
            //
            int qrW = 170;
            int x = (w - qrW) >> 1;
            int y = (h - qrW) >> 1;
            Image image = null;
            try {
                image = ImageIO.read(new File("D:\\data\\genTmp\\01012000279.png"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            g2d.drawImage(image, x, y, qrW, qrW, null);

            String pileNo = "桩编号：01012000279_01";
            String gunNo = "枪  号：01 #";

            int tx;
            int ty = 380;
            g2d.setPaint(Color.black);
            Font font = new Font("楷体", Font.BOLD, 16);
            g2d.setFont(font);
            int tw = g2d.getFontMetrics().stringWidth(pileNo);
            tx = (w - tw) >> 1;
            g2d.drawString(pileNo, tx, ty);
            ty += 30;
            g2d.drawString(gunNo, tx, ty);
            try {
                Image wxPayImage = ImageIO.read(new File("D:\\wxtalk.png"));
                g2d.setPaint(Color.white);
                RoundRectangle2D roundRectangle2D = new RoundRectangle2D.Double((w - boxW) >> 1, 30, 48, 48, 20, 20);
                g2d.fill(roundRectangle2D);
                g2d.drawImage(wxPayImage, (w - boxW) >> 1, 30, 48, 48, null);
                //
                Image aliPayImage = ImageIO.read(new File("D:\\alipay.png"));
                g2d.setPaint(Color.white);
                roundRectangle2D = new RoundRectangle2D.Double((((h - boxW - 48)  )), 30, 48, 48, 24, 24);
                g2d.fill(roundRectangle2D);
                g2d.drawImage(aliPayImage, (((h - boxW - 48)  )), 30, 48, 48, null);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            g2d.dispose();
        }
    }


}
